app.controller('bpmdefinitionController', ['$scope', 'httpWrapService', '$state', '$stateParams','uiGridConstants','GridFactory','dialogs',
        function($scope, httpWrapService, $state, $stateParams,uiGridConstants, GridFactory,dialogs) {

    $scope.datapickerOption = {
        format : 'YYYY-MM-DD HH:mm:ss'
    }

    $scope.load = function(deploymentId){
        httpWrapService.post('bpm/deployment/list.do').then(function (res) {
            $scope.definitions = res.data.data;
            $scope.$watch('definitions', function(){
                $('.footable').trigger('footable_redraw');
            });
            //setTimeout(function(){
            //        $('.footable').trigger('footable_redraw');
            //    },
            //100)
        });
    }
    $scope.load();

    $scope.delete = function(deploymentId){
        httpWrapService.defaultDelete('bpm/deployment/delete/' + deploymentId +'.do',{},'确认删除流程定义吗？删除后不能恢复，是否确认删除').then(function (res) {
            $scope.load();
        });
    }

    $scope.getDefaultDate = function(){
        return moment().add(1,'h').startOf('hour').toDate();
    }

    $scope.active = function(id){
        if(!$scope.datapickerOption.defaultDate) {
            $scope.datapickerOption.defaultDate = $scope.getDefaultDate();
        }
        var data = {
            status : 'active',
            processDefinitionId : id
        };
        data.option = $scope.datapickerOption;
        var dlg = dialogs.create('tpl\/bpm\/manager\/processDefinition\/changeState.html','custombpmManagerDefinitionDialogCtrl',data,{size:'md',keyboard: true,backdrop: true,windowClass: 'my-class'});
        dlg.result.then(function(entity){
            httpWrapService.defaultPost('bpm/deployment/active.do',entity).then(function(){
                $scope.load();
            });
        },function(text){
        });
    }
    $scope.suspend = function(id){
        if(!$scope.datapickerOption.defaultDate) {
            $scope.datapickerOption.defaultDate = $scope.getDefaultDate();
        }
        var data = {
            status : 'suspend',
            processDefinitionId : id
        };
        data.option = $scope.datapickerOption;
        var dlg = dialogs.create('tpl\/bpm\/manager\/processDefinition\/changeState.html','custombpmManagerDefinitionDialogCtrl',data,{size:'md',keyboard: true,backdrop: true,windowClass: 'my-class'});
        dlg.result.then(function(entity){
            httpWrapService.defaultPost('bpm/deployment/suspend.do',entity).then(function(){
                $scope.load();
            });
        },function(text){
        });
    }
}]);


app.controller('custombpmManagerDefinitionDialogCtrl',['$scope', '$uibModalInstance','data', function($scope,$uibModalInstance,data){
    //-- Variables --//
    $scope.data = data;
    $scope.entity = {};
    $scope.status = data.status;
    $scope.entity.processDefinitionId = data.processDefinitionId;
    //-- Methods --//

    $scope.cancel = function(){
        $uibModalInstance.dismiss('Canceled');
    }; // end cancel

    $scope.ok = function(){
        $uibModalInstance.close($scope.entity);
    }; // end save

}]);